package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.ApplicationV2DO;
import com.example.makerspace.domain.CompetitionDO;
import com.example.makerspace.domain.UserCompetitionRelationShipDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.dto.Result;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.mapper.ResourceMapper;
import com.example.makerspace.mapper.UserCompetitionRelationShipMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class UserCompetitionRelationShipDAO extends BaseDAO<UserCompetitionRelationShipDO>{
    @Resource
    private UserCompetitionRelationShipMapper userCompetitionRelationShipMapper;

    @PostConstruct
    public void init(){
        this.mapper = userCompetitionRelationShipMapper;
    }
    public UserCompetitionRelationShipDO checkCompetition(String username, Integer id){
        QueryWrapper<UserCompetitionRelationShipDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("competition_id",id);
        return userCompetitionRelationShipMapper.selectOne(queryWrapper);
    }

    public List<UserCompetitionRelationShipDO> selectByCompetitionId(String username,Integer competitionId) {
        QueryWrapper<UserCompetitionRelationShipDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("competition_id",competitionId);
        return  userCompetitionRelationShipMapper.selectList(queryWrapper);
    }

    public List<UserCompetitionRelationShipDO> selectByUsername(String username) {
        QueryWrapper<UserCompetitionRelationShipDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
         return   userCompetitionRelationShipMapper.selectList(queryWrapper);
    }

    public void deleteByUsernameAndCompetitionId(String username, Integer competitionId) {
        QueryWrapper<UserCompetitionRelationShipDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("competition_id",competitionId);
        userCompetitionRelationShipMapper.delete(queryWrapper);
    }
    public void updateByUserNameAndCompetitionId(UserCompetitionRelationShipDO userCompetitionRelationShipDO,String username, Integer competitionId) {
        UpdateWrapper<UserCompetitionRelationShipDO> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("username",username).eq("competition_id",competitionId);
        userCompetitionRelationShipMapper.update(userCompetitionRelationShipDO,queryWrapper);

    }

    public void deleteByUsername(String username) {
        userCompetitionRelationShipMapper.delete(new QueryWrapper<UserCompetitionRelationShipDO>().eq("username",username).orderByDesc("modify_time"));
    }

    public List<UserCompetitionRelationShipDO> selectByWaitRelation() {
        UpdateWrapper<UserCompetitionRelationShipDO> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("state","审核中");
        return  mapper.selectList(queryWrapper);
    }

    public List<UserCompetitionRelationShipDO> selectBySuccessRelation() {
        UpdateWrapper<UserCompetitionRelationShipDO> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("state","赛题报名成功");
        return  mapper.selectList(queryWrapper);
    }

    public List<UserCompetitionRelationShipDO> selectByFailRelation() {
        UpdateWrapper<UserCompetitionRelationShipDO> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("state","赛题报名失败");
        return  mapper.selectList(queryWrapper);
    }


}
